import requests

url = 'https://www.yuque.com/api/v2'
# 用户个人路径;团队名
login = '/dxz99wyr'
# 知识库名称
book_slug = '/pythonapi'
# 仓库唯一名称, 由 个人路径+知识库名 组成
namespace = '/dxz99wyr/pythonapi'
slug = '/20221026'
# 20221026 文章id
id = '/98292914'


headers = {
    "Content-Type": "application/json",
    "User-Agent": 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:68.0) Gecko/20100101 Firefox/68.0'
}

# 获取单个用户信息
def get_user_info(token:str,login:str)->dict:
    """
    获取单个用户信息
    :param token:获取地址:https://www.yuque.com/settings/tokens/new
    :param login: str,用户个人路径;ex:/dxz99wyr
    :return:dict
    """
    headers['X-Auth-Token'] = token
    r = requests.get(url + '/users' + login, headers=headers)
    return r.json()

# 获取认证的用户的个人信息
def get_active_user_info(token:str)->dict:
    """
    :param token:获取地址:https://www.yuque.com/settings/tokens/new
    :return:dict
    """
    headers['X-Auth-Token'] = token
    r = requests.get(url + '/user', headers=headers)
    return r.json()

# 获取某个用户的加入的组织列表
def get_user_join_group_list(token:str,login:str)->dict:
    """
    获取某个用户的加入的组织列表
    :param token:获取地址:https://www.yuque.com/settings/tokens/new
    :param login: str,用户个人路径;ex:/dxz99wyr
    :return:dict
    """
    headers['X-Auth-Token'] = token
    r = requests.get(url + '/users' + login + '/groups',headers=headers)
    return r.json()

# 获取公开组织列表
def get_public_group_list(token)->dict:
    """
    获取公开组织列表
    :param token:获取地址:https://www.yuque.com/settings/tokens/new
    :return:dict
    """
    headers['X-Auth-Token'] = token
    r = requests.get(url + '/groups',headers=headers)
    return r.json()

# # 创建组织XXX
# def create_group(data):
#     r = requests.post(url + '/groups', headers=headers, json=data)
#     print(r.text)
#
# def get_group_info():
#     r = requests.get(url + '/groups' + login,headers=headers)
#     print(r.json())

# 获取某个用户/团队的知识库列表
def get_user_knowledge_base(token,login,data)->dict:
    """
    获取某个用户/团队的知识库列表
    :param token:获取地址:https://www.yuque.com/settings/tokens/new
    :param login: str,用户个人路径;ex:/dxz99wyr
    :param data: dict,参数为：type:str,offset:int
                        type取值为：Book,Design,all
    :return: dict
    """
    headers['X-Auth-Token'] = token
    r = requests.get(url + '/users' + login + '/repos', headers=headers, json=data)
    print(r.json())

# 创建知识库
def create_knowledge_base(token,login,owner,data)->dict:
    """
    创建知识库
    :param token:获取地址:https://www.yuque.com/settings/tokens/new
    :param login: str,用户个人路径;ex:/dxz99wyr
    :param owner: str,所有者.取值为 users 或 groups
    :param data: dict,参数为:name,slug,description,public,type
                        public取值为：0 私密,1 所有人可见,2 空间成员可见,3 空间所有人可见,4 知识库成员可见
                        type取值为：Book,Design
    :return: dict
    """
    headers['X-Auth-Token'] = token
    if owner == 'users':
        r = requests.post(url + '/users' + login + '/repos', headers=headers, json=data)
        return r.json()
    elif owner == 'groups':
        r = requests.post(url + '/groups' + login + '/repos', headers=headers, json=data)
        return r.json()
    else:
        return False

# 获取知识库详情
def get_knowledge_base_info(token,namespace,data)->dict:
    """
    获取知识库详情
    :param token:获取地址:https://www.yuque.com/settings/tokens/new
    :param namespace: str,个人路径+知识库名;ex:/dxz99wyr/pythonapi
    :param data: dict,参数为type:str.取值为：Book - 文库，Design - 设计稿
    :return: dict
    """
    headers['X-Auth-Token'] = token
    r = requests.get(url + '/repos' + namespace, headers=headers, json=data)
    return r.json()

# 更新知识库信息
def put_knowledge_base_info(token,namespace,data):
    """
    更新知识库信息
    :param token:获取地址:https://www.yuque.com/settings/tokens/new
    :param namespace: str,个人路径+知识库名;ex:/dxz99wyr/pythonapi
    :param data:dict,参数为：title:str,slug:str,toc:str,description:str,public:int
                public取值：0 私密,1 所有人可见,2 空间成员可见，3 空间所有人（含外部联系人）可见，4 知识库成员可见
    :return:dict
    """
    headers['X-Auth-Token'] = token
    r = requests.put(url + '/repos' + namespace, headers=headers, json=data)
    return r.json()

# 删除知识库
def del_knowledge_base(token,namespace):
    """
    删除知识库
    :param token:获取地址:https://www.yuque.com/settings/tokens/new
    :param namespace: str,个人路径+知识库名;ex:/dxz99wyr/pythonapi
    :return: dict
    """
    headers['X-Auth-Token'] = token
    r = requests.delete(url + '/repos' + namespace,headers=headers)
    return r.json()

# 获取一个仓库的文档列表
def get_knowledge_base_docs(token,namespace):
    """
    获取一个仓库的文档列表
    :param token:获取地址:https://www.yuque.com/settings/tokens/new
    :param namespace: str,个人路径+知识库名;ex:/dxz99wyr/pythonapi
    :return: dict
    """
    headers['X-Auth-Token'] = token
    r = requests.get(url + '/repos' + namespace + '/docs',headers=headers)
    return r.json()

# 获取单篇文档的详细信息
def get_doc_info(token:str,namespace:str,slug:str)->dict:
    """
    获取单篇文档的详细信息
    :param token:获取地址:https://www.yuque.com/settings/tokens/new
    :param namespace: str,个人路径+知识库名;ex:/dxz99wyr/pythonapi
    :param slug:str,文章唯一路径;ex:/20221026
    :return:dict
    """
    headers['X-Auth-Token'] = token
    r = requests.get(url + '/repos' + namespace + '/docs' + slug, headers=headers)
    return r.json()

# 创建文档
def create_doc(token,namespace,data):
    """
    :param token:获取地址:https://www.yuque.com/settings/tokens/new
    :param namespace: str,个人路径+知识库名;ex:/dxz99wyr/pythonapi
    :param data:dict,参数为：title,slug,format,body;其中format取值可为：markdown、lake、html
    :return: dict
    """
    headers['X-Auth-Token'] = token
    r = requests.post(url + '/repos' + namespace + '/docs',headers=headers,json=data)
    return r.json()

# 更新文档
def put_doc(token,namespace,id,data):
    """
    更新文档
    :param token:获取地址:https://www.yuque.com/settings/tokens/new
    :param namespace: str,个人路径+知识库名;ex:/dxz99wyr/pythonapi
    :param id: str,文档唯一标识，通过get_doc_info()获取;ex:'/98292914'
    :param data:dict,参数为:title,slug,body,_force_asl
    :return: dict
    """
    headers['X-Auth-Token'] = token
    r = requests.put(url + '/repos' + namespace + '/docs' + id,headers=headers,json=data)
    return r.json()

# 删除文档
def del_doc(token,namespace,id):
    """
    删除文档
    :param token:获取地址:https://www.yuque.com/settings/tokens/new
    :param namespace: str,个人路径+知识库名;ex:/dxz99wyr/pythonapi
    :param id: str,文档唯一标识，通过get_doc_info()获取;ex:'/98292914'
    :return: dict
    """
    headers['X-Auth-Token'] = token
    r = requests.delete(url + '/repos' + namespace + '/docs' + id,headers=headers)
    return r.json()


if __name__ == '__main__':

    # # 获取单个用户信息
    # get_user_info = get_user_info()
    #
    # # 获取认证的用户的个人信息
    # get_active_user_info = get_active_user_info()
    #
    # # 获取某个用户的加入的组织列表
    # get_user_join_group_list()
    #
    # # 获取公开组织列表
    # get_public_group_list()

    # # 创建group
    # data = {
    #     'name':'20221026-1用于Python',
    #     'login':login.split('/')[1],
    #     'description':'临时用11'
    # }
    # create_group(data=data)
    #
    # # 获取单个组织的详细信息
    # get_group_info()

    # # 获取某个用户的知识库列表
    # data = {
    #     # string;Book, Design, all
    #     'type': 'all',
    #     # integer;用于分页，效果类似 MySQL 的 limit offset，一页 20 条
    #     'offset': 3,
    # }
    # get_user_knowledge_base(data)


    # # 创建知识库
    # data = {
    #     'name':'20221026-1临时创建的知识库',
    #     'slug':'111',
    #     'description':'临时使用的',
    #     'public':0, #0 私密,
    #                 # 1 所有人可见,
    #                 # 2 空间成员可见，
    #                 # 3 空间所有人（含外部联系人）可见\
    #                 # 4 知识库成员可见
    #     'type':'Book',  #‘Book’ 文库,
    #                     # ‘Design’ 画板, 请注意大小写
    #
    # }
    # create_knowledge_base('users',data)

    # 获取知识库信息
    # data = {
    #     'type':'book'
    # }
    # get_knowledge_base_info(data)

    # # 更新知识库信息
    # data = {
    #     'name': '20221026临时创建的知识库',
    #     'slug': '11',
    #     'toc': '001',
    #     'description':'这是更新后的信息',
    #     'public': 0
    #
    # }
    # put_knowledge_base_info(data)

    # 删除知识库
    # del_knowledge_base()

    # 获取一个仓库的文档列表
    # get_knowledge_base_docs()

    # 获取单篇文档的详细信息
    # get_doc_info()

    # 创建文档
    # data = {
    #     'title':'20221026临时用-创建的文档',
    #     'slug':'20221026',
    #     'format':'html',
    #     'body':'1.html',
    # }
    # create_doc(data)

    # 更新文档
    # data = {
    #     'title':'20221026-更新后',
    #     'slug':'1027',
    #     'body':"这是更新后的内容",
    #     '_force_asl':1
    # }
    # put_doc(data)

    # 删除文档
    # del_doc()

    # # 搜索
    # data = {
    #     'type':'doc',
    #     'q':'网络',
    #     'offset':3,
    #     'scope':'/',
    #     'related':True
    # }
    # search_docs(data)
    pass